package com.haiyi.software.xmpp.service;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.OrFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.haiyi.software.app.MainApplication;
import com.haiyi.software.tools.L;
import com.haiyi.software.xmpp.event.XMPPConnectionState;

public class HIChatServices extends HIChatServicesXMPPOperation implements
		PacketListener {

	private static final String TAG = "LocalService";
	private IBinder binder = new HIChatServices.LocalBinder();

	@Override
	public IBinder onBind(Intent intent) {
		// TODO 自动生成的方法存根
		return binder;
	}

	// 定义内容类继承Binder
	public class LocalBinder extends Binder {
		// 返回本地服务
		HIChatServices getService() {
			return HIChatServices.this;
		}
	}

	@Override
	public void onDestroy() {

		super.onDestroy();
	}

	@Override
	public void onCreate() {
		// TODO 自动生成的方法存根
		super.onCreate();
		app.setmXMPPStream(this);

		XMPPConnection.DEBUG_ENABLED = false;
		connConfig = new ConnectionConfiguration(SERVERIP, PORT, "com.39du5");
		// connConfig.setTLSEnabled(false);
		connConfig.setCompressionEnabled(false);
		connConfig.setSelfSignedCertificateEnabled(false);
		connConfig.setSASLAuthenticationEnabled(false);
		// connConfig.setSecurityMode(SecurityMode.disabled);

		connConfig.setSendPresence(false);

		// connConfig.setVerifyChainEnabled(false);
		/** 是否启用调试 */
		connConfig.setDebuggerEnabled(false);
		connection = new XMPPConnection(connConfig);

		PacketFilter filter2 = new MessageTypeFilter(Message.Type.chat);
		PacketFilter filter1 = new MessageTypeFilter(Message.Type.normal);
		OrFilter orFilter = new OrFilter();
		orFilter.addFilter(filter1);
		orFilter.addFilter(filter2);
		connection.addPacketListener(this, orFilter);

	}

	@Override
	public synchronized XMPPConnection GetXMPPConnection() {
		// TODO 自动生成的方法存根
		return connection;
	}

	@Override
	public synchronized ConnectionConfiguration GetconnConfig() {
		// TODO 自动生成的方法存根
		return connConfig;
	}

	@Override
	public synchronized Boolean connection(String username, String password) {
		// TODO 自动生成的方法存根
		this.xmppConnection(username, password);
		return true;
	}

	@Override
	public void processPacket(Packet packet) {

		// TODO 自动生成的方法存根
		Message message = (Message) packet;
		// TODO: 这里接收到的扩展消息格式不正确

		L.i(message.toXML());

		if (message.getBody() != null) {

			// TODO 存数据库
			AddReceivedMessageToDB(message);
			app.XMPPReceiveMessage(message);
		}

	}

	@Override
	public void xmppLoginSucces(String str) {

		connection.addConnectionListener(this);

		app.XMPPAuthenticate(connection);

	}

	@Override
	public void connectionClosed() {
		// TODO 自动生成的方法存根

	}

	@Override
	public void connectionClosedOnError(Exception e) {
		// TODO 自动生成的方法存根

		MainApplication
				.getInstance()
				.getmXMPPConnEvent()
				.get(0)
				.XMPPConnectionStateListener(
						XMPPConnectionState.ConnectionShutDonw);

	}

	@Override
	public void reconnectingIn(int seconds) {
		// TODO 自动生成的方法存根

	}

	@Override
	public void reconnectionSuccessful() {
		// TODO 自动生成的方法存根

	}

	@Override
	public void reconnectionFailed(Exception e) {
		// TODO 自动生成的方法存根

	}

}
